import i18n from '@/i18n'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { MetaResourceModelBatchSaveItemDto } from './dto/meta-resource-model-batch-save.dto'

@Injectable()
export class MetaResourceModelService {
  /**
   * 生成批量添加模型的数据
   * @param items
   * @param relative
   * @returns
   */
  buildBatchSaveModels(
    items: MetaResourceModelBatchSaveItemDto[],
    relative: {
      type: DomainModel.MetaResourceModelType
      relativeType: DomainModel.MetaResourceRelativeType
      relativeId: number
    }
  ) {
    const models: Prisma.MetaResourceModelCreateManyInput[] = []

    const names: string[] = []
    const labels: string[] = []
    items.forEach((model, index) => {
      Assist.threw(
        !model.name,
        i18n.t('app-meta.meta-resource-model.meta-resource-model.12150131-0', { args: [index + 1] })
      )
      Assist.threw(
        !model.label,
        i18n.t('app-meta.meta-resource-model.meta-resource-model.12150131-1', { args: [index + 1] })
      )
      Assist.threw(
        names.includes(model.name),
        i18n.t('app-meta.meta-resource-model.meta-resource-model.12150131-2', { args: [model.name] })
      )
      Assist.threw(
        labels.includes(model.label),
        i18n.t('app-meta.meta-resource-model.meta-resource-model.12150131-3', { args: [model.label] })
      )

      names.push(model.name)
      labels.push(model.label)
      models.push({
        ...relative,
        ...model,
      })
    })

    return models
  }
}
